const axios = require("axios").default; //因为用到的是ES6规范,不是commonJS规范
const cheerio = require("cheerio");
const Book = require("../models/bokecontent");

// 1.获取杨青青博客网页源代码的所有html信息
async function getbolgHTML() {
    const resp = await axios.get("https://blog.csdn.net/qq_46075739?spm=1000.2115.3001.5343")
    // console.log(resp)
    return resp.data;
}
// getbolgHTML()

// 2.得到每篇博客的详情链接,转换成数组
async function getbolgLinks() {
     const html = await getbolgHTML()
     const $ = cheerio.load(html)
     const achorElements = $(".blog-list-box a")//路径
    //console.log(achorElements[0])
     const links = achorElements.map((i, ele) => {
        const href = ele.attribs["href"]
        return href
    }).get() //将类数组转换成数组
    console.log(links)
    return links
}
// getbolgLinks()
// 3.根据博客地址,得到书籍的详细信息
async function getbolgDetail(dedailUrl) {
    const resp = await axios.get(dedailUrl)
    const $ = cheerio.load(resp.data)
    const article = $(".blog-content-box").html().trim()//获取文章
    console.log(article)
    return {
        article
    }
}

// getbolgDetail("https://www.tjcblog.com/blog/64.html").then((data) => {
//      console.log(data)
// })
// 获取所有的博客信息
async function fetchAll() {
    const links = await getbolgLinks()
    const proms = links.map((link) => {
        return getbolgDetail(link)
    })
    return Promise.all(proms)
}

// 得到博客信息保存数据库
async function saveToDB() {
    const books = await fetchAll()
    console.log(books)
    await Book.bulkCreate(books)
    console.log("数据已保存")
}
saveToDB()